package edu.maping.easy.painter.plugins;

import data.NAppletManager;
import napplet.EMDrawer;
import napplet.EMNAppletManager;
import napplet.InterfazPlayPause;
import processing.core.PApplet;
import edu.maping.easy.ControlFacade;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GUIEvent;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.gui.Slider;
import edu.maping.easy.model.PhysicPolyThing;
import edu.maping.easy.model.Point;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.painter.EventPainter;
import edu.maping.easy.painter.ThingPainterDelegate;
import edu.maping.easy.painter.UtilPainter;
import edu.maping.easy.sequence.ActionConf;
import edu.maping.easy.sequence.ticker.TickerListener;

public class ThingPainterRectangleTransform implements ThingPainterDelegate,
		TickerListener, EMDrawer,InterfazPlayPause {

	int moment = 0;

	boolean asc = false;

	int time = 0;

	private UtilPainter utilPainter = new UtilPainter();

	int blue = 0;
	int green = 0;
	int red = 0;
	int decay = 5;
	int rise = 20;

	Thing thing;
	PApplet pApplet;
	EMNAppletManager nappletManager;

	@Override
	public void draw() {
		draw(thing, pApplet, null);
	}

	public void draw(Thing cosa, PApplet pApplet, Object object) {

		time++;
		
		if(cosa == null){
			return;
		}

		if (cosa.isEvent()) {
			turnOn();
			cosa.setEvent(false);
		}

		if (asc) {
			if (moment < 255) {
				moment += rise;
			} else {
				asc = false;
				moment = 255;
			}
		} else {
			if (moment > 0) {
				moment -= decay;
			} else {
				moment = 0;
				return;
			}
		}

		float tempR = (red / 255F) * moment;
		float tempG = (green / 255F) * moment;
		float tempB = (blue / 255F) * moment;

		PolygonThing poly = (PolygonThing) cosa;
		pApplet.pushStyle();
		
		pApplet.strokeWeight(0);
		pApplet.stroke(0);
		pApplet.fill(tempR, tempG, tempB);
		poly.draw(pApplet);
//		pApplet.beginShape();
//		for (int i = 0; i < poly.getVertex().size(); i++) {
//			Point point = poly.getVertex().get(i);
//			pApplet.vertex(point.x, point.y);
//		}
//		pApplet.endShape();
		pApplet.popStyle();

	}

	public void draw(Thing body, PApplet pApplet) {
		draw(body, pApplet, null);
	}

	public void turnOn() {
		asc = true;
	}

	@Override
	public void setConf(ActionConf actionConf) {
		for (int i = 0; i < utilPainter.size(); i++) {
			Slider slider = (Slider) utilPainter.get(i);
			String key = slider.name;
			if (actionConf.containsKey(key)) {
				float value = actionConf.get(key);
				setParameters(key, value);
				utilPainter.setParameter(key, value);
			} else {
				utilPainter.setParameter(key, 0);
			}
		}

	}

	@Override
	public String getName() {
		return "PulsoLigth";
	}

	@Override
	public EventPainter controlEvent(GUIEvent theEvent) {
		EventPainter eventPainter = utilPainter.controlEvent(theEvent);
		if (eventPainter != null)
			setParameters(eventPainter.name, eventPainter.value);
		return eventPainter;
	}

	private void setParameters(String name, float value) {

		if (name.equals("RED")) {
			red = (int) value;
		} else if (name.equals("GREEN")) {
			green = (int) value;
		} else if (name.equals("BLUE")) {
			blue = (int) value;
		} else if (name.equals("DECAY")) {
			decay = (int) value;
		} else if (name.equals("RISE")) {
			rise = (int) value;
		}
	}

	@Override
	public void setup(PApplet pApplet, ControlFacade controlFacade,
			ButtonList buttonList, GuiFactory guiFactory) {

		this.pApplet = pApplet;
		utilPainter.setup(pApplet, controlFacade, buttonList, guiFactory);
		utilPainter.add("RED", 0, 255);
		utilPainter.add("GREEN", 0, 255);
		utilPainter.add("BLUE", 0, 255);
		utilPainter.add("DECAY", 1, 40);
		utilPainter.add("RISE", 1, 80);

	}

	@Override
	public void showForm(int x, int y) {
		utilPainter.setPosition(x, y);
		utilPainter.render();
	}

	@Override
	public void hideForm() {
		utilPainter.hide();
	}

	public ActionConf createNewActionConf() {
		return utilPainter.createActionConf();
	}

	@Override
	public ThingPainterDelegate createNewInstance() {
		ThingPainterRectangleTransform contournTransform = new ThingPainterRectangleTransform();
		contournTransform.pApplet = this.pApplet;
		contournTransform.utilPainter = utilPainter.clone();
		nappletManager.addEmbeddedEMDrawer(contournTransform);
		return contournTransform;
	}

	@Override
	public void setThing(Thing thing) {
		// TODO Auto-generated method stub
		this.thing = thing;
	}

	@Override
	public void setNappletManager(EMNAppletManager manager) {
		this.nappletManager = manager;
	}
	
	boolean running = false;
	
	@Override
	public boolean isRunning() {
		return running;
	}

	@Override
	public void play() {
		running = true;
	}

	@Override
	public void pause() {
		running = false;
	}

}
